'use strict';
app.controller('agentWithdrawalsCtrl',['$scope','utilService','$modal', function($scope, utilService, $modal){
    $scope.withdrawals = {};
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domainAgent + 'manage/walletWithdrawalsRecord/getWithdrawalsList',
            params:$scope.withdrawals
        }).then(function(result){});
    }
    $scope.query();

    // 详细
    $scope.openDetailed = function (withdrawals) {
        $scope.detailedWin = $modal.open({
            templateUrl: 'tpl/agent_withdrawals/agent_withdrawals_detailed.html',
            controller: 'withdrawalsDetailedCtrl',
            scope:$scope,
            resolve: {
                withdrawals: function() {
                    return withdrawals
                }
            }
        });
    };
}]);


app.controller('withdrawalsDetailedCtrl', ['$scope', '$modalInstance', 'agentWithdrawalsService', 'toaster', 'withdrawals', 'utilService', '$modal',
    function($scope, $modalInstance, agentWithdrawalsService, toaster, withdrawals, utilService, $modal) {

        $scope.withdrawals = {
            id: withdrawals.id,
            wallet:{
                balance: 0,
                totalAmount: 0,
                totalWithdrawals: 0
            }
        };

        $scope.id = withdrawals.id;
        $scope.init = function () {
            agentWithdrawalsService.withdrawalsDetailed($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        var withdrawals = result.data;
                        $scope.withdrawals.agentArea = withdrawals.agentArea;
                        $scope.withdrawals.agentName = withdrawals.agentName;
                        $scope.withdrawals.amount = withdrawals.amount;
                        $scope.withdrawals.bankCard = withdrawals.bankCard;
                        $scope.withdrawals.failureRemarks = !withdrawals.failureRemarks ? '' : withdrawals.failureRemarks;
                        $scope.withdrawals.mobile = withdrawals.mobile;
                        $scope.withdrawals.remarks = withdrawals.remarks;
                        $scope.withdrawals.status = withdrawals.status;
                        $scope.withdrawals.wallet = {
                            balance: withdrawals.wallet.balance,
                            totalAmount: withdrawals.wallet.totalAmount,
                            totalWithdrawals: withdrawals.wallet.totalWithdrawals,
                        }
                        break;
                    default :

                        break;
                }
            });
        }
        $scope.init();

        $scope.pass = function (withdrawals) {
            $scope.modal = {
                title: "审核申请",
                message: "确定通过'"+ withdrawals.amount +"'金额的提现申请？",
            };
            utilService.modal($modal, $scope, {
                title: $scope.modal.title,
                controller: "withdrawalsPassCtrl",
                message: $scope.modal.message,
                data:{
                    id: $scope.id
                }
            });
        }

        $scope.withdrawalsSuccess = function (withdrawals) {
            $scope.modal = {
                title: "提现成功",
                message: "确定已成功转账'"+ withdrawals.amount +"'给代理商了？",
            };
            utilService.modal($modal, $scope, {
                title: $scope.modal.title,
                controller: "withdrawalsSuccessCtrl",
                message: $scope.modal.message,
                data:{
                    id: $scope.id
                }
            });
        }

        $scope.withdrawalsFailure = function (withdrawals) {
            $modal.open({
                templateUrl: 'tpl/agent_withdrawals/agent_withdrawals_failure_remarks.html',
                controller: 'withdrawalsFailureCtrl',
                scope:$scope,
                resolve: {
                    withdrawals: function() {
                        return withdrawals
                    }
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

app.controller('withdrawalsPassCtrl', ['$scope', 'toaster', '$modalInstance', 'modalData', 'agentWithdrawalsService',
    function($scope, toaster, $modalInstance, modalData, agentWithdrawalsService) {

        $scope.isAllowModal = true;

        //确认
        $scope.ok = function(){
            if(!$scope.isAllowModal){
                return;
            }
            $scope.isAllowModal = false;
            agentWithdrawalsService.pass($scope, modalData.data.id).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "审核成功");
                        $scope.query();
                        $scope.detailedWin.close();
                        $scope.cancel();
                        break;
                    default :
                        toaster.pop('error', "审核通过操作失败", result.message);
                        break;
                }
                $scope.isAllowModal=true;
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

app.controller('withdrawalsFailureCtrl', ['$scope', 'toaster', '$modalInstance', 'withdrawals', 'agentWithdrawalsService',
    function($scope, toaster, $modalInstance, withdrawals, agentWithdrawalsService) {

        $scope.id = withdrawals.id;
        $scope.content = '';
        $scope.submit = function (form) {
            if(!form.$valid) {
                // 表单验证没通过
                return;
            }

            agentWithdrawalsService.failure($scope).then(function (result) {
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.query();
                        $scope.detailedWin.close();
                        $scope.cancel();
                        break;
                    default :
                        toaster.pop('error', "审核不通过操作失败", result.message);
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('withdrawalsSuccessCtrl', ['$scope', 'toaster', '$modalInstance', 'modalData', 'agentWithdrawalsService',
    function($scope, toaster, $modalInstance, modalData, agentWithdrawalsService) {

        $scope.isAllowModal = true;

        //确认
        $scope.ok = function(){
            if(!$scope.isAllowModal){
                return;
            }
            $scope.isAllowModal = false;
            agentWithdrawalsService.success($scope, modalData.data.id).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "提现成功");
                        $scope.query();
                        $scope.detailedWin.close();
                        $scope.cancel();
                        break;
                    default :
                        toaster.pop('error', "提现成功操作失败", result.message);
                        break;
                }
                $scope.isAllowModal=true;
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);